mui.init();

document.addEventListener("swipeleft", function () {
    console.log('左');
    var self = searchDataList;
    if(self.tabIndex < 4){
        self.tabIndex ++;
    }else{
        self.tabIndex = 4
    }
});

document.addEventListener("swiperight", function () {
    console.log('右');
    var self = searchDataList;
    if(self.tabIndex > 1){
        self.tabIndex --;
    }else{
        self.tabIndex = 1
    }
});

var searchDataList;
mui.plusReady(function () {
    var keywords = plus.webview.currentWebview().keywords;
    console.log('接受到的参数：'+keywords);
    if (keywords == '') {
        mui.toast('您没有输入搜索关键词', {duration: '2000',type: 'div'});
        return false;
    }

    searchDataList = new Vue({
        el: '.content',
        data: {
            userWords: keywords ? keywords : '',
            page:1,
            npage:1,
            cpage:1,
            epage:1,
            spage:1,

            dataUlList:0,

            ncount:0,
            ccount:0,
            ecount:0,
            scount:0,

            article_list: [],
            course_list: [],
            ebook_list: [],
            shop_list: [],
            tabIndex: 1,
            tabName: 'news',

            miniRefreshArr:[],
            miniRefresh1: null,
            miniRefresh2: null,
            miniRefresh3: null,
            miniRefresh4: null,

            auditStatus:'no',
        },
        watch:{
            tabName:function (val,oldVal) {

            },
            tabIndex:function (val,oldVal) {
                var self = this;
                if(val == 1){
                    self.tabName = 'news';
                }else if(val == 2){
                    self.tabName = 'class';
                }else if(val == 3){
                    self.tabName = 'ebook';
                }else if(val == 4){
                    self.tabName = 'shop';
                }
                console.log(self.miniRefreshArr[val-1]);
                if(self.miniRefreshArr[val-1] == undefined){
                    self.$nextTick(function () {
                        self.miniRefreshArr[val-1] = new MiniRefresh({
                            container: '#minirefresh' + val,
                            down: {
                                isAuto: true,
                                callback: function() {
                                    self.getSerachResult('normal');
                                }
                            },
                            up: {
                                isAuto: false,
                                offset:10,
                                loadFull:{
                                    isEnable:false,
                                },
                                callback: function() {
                                    self.getSerachResult('up');
                                }
                            }
                        });
                    })

                }else{
                    self.getSerachResult('tab');
                }
            }
        },
        mounted() {
            var self = this;
            self.auditStatus = plus.storage.getItem('auditStatus') ? plus.storage.getItem('auditStatus') : 'no';
            self.$nextTick(function () {
                self.miniRefreshArr[0] = new MiniRefresh({
                    container: '#minirefresh1',
                    down: {
                        isAuto: true,
                        callback: function() {
                            self.getSerachResult('normal');
                        }
                    },
                    up: {
                        isAuto: false,
                        offset:10,
                        loadFull:{
                            isEnable:false,
                        },
                        callback: function() {
                            self.getSerachResult('up');
                        }
                    }
                });
            })
        },
        methods: {
            getSerachResult:function(type){
                var self = this;
                if(type == 'tab'){
                    self.miniRefreshArr[self.tabIndex - 1].resetUpLoading();
                }
                console.log('操作方式:'+type);
                console.log('第几个选项卡：'+self.tabIndex);
                switch (type) {
                    case 'normal':
                        //showWatting();
                        self.article_list.length = 0;
                        self.course_list.length = 0;
                        self.ebook_list.length = 0;
                        self.shop_list.length = 0;
                        self.page = 1;
                        self.npage = 1;
                        self.cpage = 1;
                        self.epage = 1;
                        self.spage = 1;
                        self.getOnesSerachData();
                        setTimeout(function () {
                            self.miniRefreshArr[self.tabIndex - 1].endDownLoading(true);
                        },1000);
                        break;
                    case 'up':
                        switch (self.tabName) {
                            case 'news':
                                self.npage++;
                                self.page = self.npage;
                                break;
                            case 'class':
                                self.cpage++;
                                self.page = self.cpage;
                                break;
                            case 'ebook':
                                self.epage++;
                                self.page = self.epage;
                                break;
                            case 'shop':
                                self.spage++;
                                self.page = self.spage;
                                break;
                        }
                        self.getOnesSerachData('up');
                        break;
                    case 'tab':
                        switch (self.tabName) {
                            case 'news':
                                if (self.article_list.length == 0) {
                                    //showWatting();
                                    self.page = self.npage = 1;
                                    self.getOnesSerachData('tab');
                                }else{
                                    self.dataUlList = 0;
                                }
                                break;
                            case 'class':
                                if (self.course_list.length == 0) {
                                    //showWatting();
                                    self.page = self.cpage = 1;
                                    self.getOnesSerachData('tab');
                                }else{
                                    self.dataUlList = 0;
                                }
                                break;
                            case 'ebook':
                                if (self.ebook_list.length == 0) {
                                    //showWatting();
                                    self.page = self.epage = 1;
                                    self.getOnesSerachData('tab');
                                }else{
                                    self.dataUlList = 0;
                                }
                                break;
                            case 'shop':
                                if (self.shop_list.length == 0) {
                                    //showWatting();
                                    self.page = self.spage = 1;
                                    self.getOnesSerachData('tab');
                                }else{
                                    self.dataUlList = 0;
                                }
                                break;
                        }
                        break;
                }

            },
            getOnesSerachData:function(type='normal'){
                var self = this;
                var api_host = plus.storage.getItem('HOST_URL');
                console.log('当前页数：'+self.page);

                /*
                    * 此处电子书有未知BUG 访问APP后台接口会重复返回第一页数据  这只是一个暂时的解决方案
                */
                var data,url;
                if(self.tabName == 'ebook'){
                    url = 'https://xcx.gongxuanwang.com/api/wxapp/app/ebook_list';
                    data = {
                        'currentpage': self.page,
                        'keywords': self.userWords,
                        'page': 8
                    }
                }else{
                    url = api_host + '/api/other/other/search';
                    data = {
                        'currentpage': self.page,
                        'keywords': self.userWords,
                        'cat': self.tabName
                    }
                }
                //获取数据
                mui.ajax({
                    url: url,
                    data:data ,
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    timeout:5000,
                    dataType: 'json',
                    type: 'get',
                    success: function (msg) {
                        console.log('返回数据长度：'+msg.data.data.length);
                        if (msg.code == 1) {
                            self.dataUlList = 0;
                            if(msg.data.count > 0){
                                switch (self.tabName) {
                                    case 'news':
                                        $.each(msg.data.data, function (index, value) {
                                            self.article_list.push(value);
                                        })
                                        self.ncount = msg.data.count ? msg.data.count : 0;
                                        break;
                                    case 'class':
                                        $.each(msg.data.data, function (index, value) {
                                            self.course_list.push(value);
                                        })
                                        self.ccount = msg.data.count ? msg.data.count : 0;
                                        break;
                                    case 'ebook':
                                        $.each(msg.data.data, function (index, value) {
                                            self.ebook_list.push(value);
                                        })
                                        self.ecount = msg.data.count ? msg.data.count : 0;
                                        break;
                                    case 'shop':
                                        $.each(msg.data.data, function (index, value) {
                                            self.shop_list.push(value);
                                        })
                                        self.scount = msg.data.count ? msg.data.count : 0;
                                        break;
                                }
                            }else if(msg.data.count == 0){
                                self.dataUlList = 1;
                            }

                            if(msg.data.data.length == 0 && type == 'up'){
                                // console.log(JSON.stringify(self.miniRefreshArr));
                                self.miniRefreshArr[self.tabIndex - 1].endUpLoading(true);
                            }else if(msg.data.data.length > 0 && type == 'up'){
                                self.miniRefreshArr[self.tabIndex - 1].endUpLoading(false);
                            }
                            //如果是加载第一页并且返回数据长度为0 则显示无数据
                            if(self.page == 1 && msg.data.data.length == 0){
                                self.dataUlList = 1;
                            }
                        } else{
                            mui.toast(msg.msg, {duration: '200',type: 'div'});
                        }
                    },
                    error: function () {
                        mui.toast('加载失败!', {duration: '200',type: 'div'});
                    },
                    complete: function () {
                        plus.nativeUI.closeWaiting();
                        self.$nextTick(function () {
                            var slider = mui("#slider");
                            slider.slider({
                                interval: 0
                            });
                        })
                    }
                })
            },
            //打开文章详情页
            open_details: function (id) {
                mui.openWindow({
                    url: '../news/detail.html',
                    extras: {
                        'newsid': id
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            //打开课程详情页
            open_course: function (classid) {
                mui.openWindow({
                    url: '../courseDetail/courseDetail.html',
                    extras: {
                        'cid': classid
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            //打开电子书详情页
            open_book_detils: function (bookid) {
                mui.openWindow({
                    url: '../eBook/eBookDetail.html',
                    extras: {
                        'bookid': bookid
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            //打开商城详情页
            open_shop: function (id) {
                mui.openWindow({
                    url: '../courseDetail/mallDetail.html',
                    extras: {
                        'product_id': id
                    },
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                });
            },
            select_tab: function (e) {
                this.tabIndex = e.target.getAttribute('tab-index');
                this.tabName = e.target.getAttribute('tab-name');
                // this.getSerachResult('tab');
            },
            backHOME:function () {
                mui.openWindow({
                    url: '../tab/home.html',
                    createNew: false,
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                    }
                })
            }
        }
    })
})

//左上角箭头返回事件
mui('.mui-bar-nav').on('tap','.mui-action-back',function () {
    mui.openWindow({
        url: '../tab/home.html',
        createNew: false,
        show: {
            autoShow: true,
            aniShow: 'pop-in',
            duration: 400
        },
        waiting: {
            autoShow: false,
        }
    })
})

//监听键盘搜索点击事件
document.getElementById("searchInput").addEventListener("keypress",function(event) {
    if(event.keyCode == "13") {
        document.activeElement.blur();//收起虚拟键盘
        searchDataList.tabIndex = 1;
        searchDataList.getSerachResult('normal');
        event.preventDefault(); // 阻止默认事件---阻止页面刷新
    }
});

function search_all() {
    //输入关键词搜索
    if ($('.searchWords_box').val() == '') {
        mui.toast('请输入您要搜索的内容', {duration: '2000',type: 'div'});
        return false;
    } else {
        $('.searchWords_box').blur();
        searchDataList.tabIndex = 1;
        searchDataList.getSerachResult('normal');
    }
}
